package chttl;

import java.io.*;

// Referenced classes of package chttl:
//            CharacterDecoder, CEFormatException, CEStreamExhausted

public class BASE64Decoder extends CharacterDecoder
{

    public BASE64Decoder()
    {
        decode_buffer = new byte[4];
    }

    int bytesPerAtom()
    {
        return 3;
    }

    int bytesPerLine()
    {
        return 57;
    }

    void decodeAtom(InputStream inputstream, OutputStream outputstream, int i)
        throws IOException
    {
        byte byte0 = -1;
        byte byte1 = -1;
        byte byte2 = -1;
        byte byte3 = -1;
        decode_buffer[0] = (byte)inputstream.read();
        if(decode_buffer[0] == -1)
            throw new CEStreamExhausted();
        int j;
        if(decode_buffer[0] == 10)
            j = readFully(inputstream, decode_buffer, 0, 4);
        else
            j = readFully(inputstream, decode_buffer, 1, 3);
        if(j == -1)
            throw new CEStreamExhausted();
        for(int k = 0; k < 64; k++)
        {
            if(decode_buffer[0] == pem_array[k])
                byte0 = (byte)k;
            if(decode_buffer[1] == pem_array[k])
                byte1 = (byte)k;
            if(decode_buffer[2] == pem_array[k])
                byte2 = (byte)k;
            if(decode_buffer[3] == pem_array[k])
                byte3 = (byte)k;
        }

        if(decode_buffer[3] == 61)
            i = decode_buffer[2] != 61 ? 2 : 1;
        if(i == 2 && decode_buffer[3] != 61)
            throw new CEFormatException("BASE64Decoder: Bad Padding byte (2).");
        if(i == 1 && (decode_buffer[2] != 61 || decode_buffer[3] != 61))
            throw new CEFormatException("BASE64Decoder: Bad Padding byte (1).");
        switch(i)
        {
        case 1: // '\001'
            outputstream.write((byte)(byte0 << 2 & 0xfc | byte1 >>> 4 & 3));
            break;

        case 2: // '\002'
            outputstream.write((byte)(byte0 << 2 & 0xfc | byte1 >>> 4 & 3));
            outputstream.write((byte)(byte1 << 4 & 0xf0 | byte2 >>> 2 & 0xf));
            break;

        case 3: // '\003'
            outputstream.write((byte)(byte0 << 2 & 0xfc | byte1 >>> 4 & 3));
            outputstream.write((byte)(byte1 << 4 & 0xf0 | byte2 >>> 2 & 0xf));
            outputstream.write((byte)(byte2 << 6 & 0xc0 | byte3 & 0x3f));
            break;
        }
    }

    void decodeLineSuffix(InputStream inputstream, OutputStream outputstream)
        throws IOException
    {
        int i;
        do
        {
            i = inputstream.read();
            if(i == -1)
                throw new CEStreamExhausted();
        } while(i != 10 && i != 13);
    }

    private static final char pem_array[] = {
        'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 
        'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 
        'U', 'V', 'W', 'X', 'Y', 'Z', 'a', 'b', 'c', 'd', 
        'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 
        'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 
        'y', 'z', '0', '1', '2', '3', '4', '5', '6', '7', 
        '8', '9', '+', '/'
    };
    byte decode_buffer[];

}
